package com.coffee.order.service.inquire.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.inquire.query.InquireOrderPlanQuery;
import com.coffee.order.api.inquire.result.InquireOrderPlanResult;
import com.coffee.order.api.inquire.service.InquireOrderPlanService;
import com.coffee.order.service.inquire.bean.InquireOrderPlan;
import com.coffee.order.service.inquire.dao.InquireOrderPlanDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class InquireOrderPlanServiceImpl implements InquireOrderPlanService {
	@Resource
	private InquireOrderPlanDao	inquireOrderPlanDao;

	@Override
	public Page<InquireOrderPlanResult> getPager(InquireOrderPlanQuery param, PagerInfo pr) {
		Page<InquireOrderPlanResult> pager=new Page<InquireOrderPlanResult>(pr.getStart(), pr.getPageSize());
		Integer count=inquireOrderPlanDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<InquireOrderPlan> list=inquireOrderPlanDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, InquireOrderPlanResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(InquireOrderPlanQuery param){
		Integer count=inquireOrderPlanDao.count(param);
		return count;
	}
	
	@Override
	public InquireOrderPlanResult getDetail(Integer id){
		InquireOrderPlan item=inquireOrderPlanDao.findById(id);
		if(item==null){
			return null;
		}
		InquireOrderPlanResult result=new InquireOrderPlanResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<InquireOrderPlanResult> getList(InquireOrderPlanQuery param, PagerInfo pr){
		List<InquireOrderPlan> list=inquireOrderPlanDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, InquireOrderPlanResult.class);
	}
	
	@Override
	public List<InquireOrderPlanResult> getList(InquireOrderPlanQuery param){
		List<InquireOrderPlan> list=inquireOrderPlanDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, InquireOrderPlanResult.class);
	}
	
	@Override
	public List<InquireOrderPlanResult> getList(){
		InquireOrderPlanQuery param = new InquireOrderPlanQuery();
		List<InquireOrderPlan> list=inquireOrderPlanDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, InquireOrderPlanResult.class);
	}


	@Override
	public void delete(Integer id) {
		inquireOrderPlanDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(InquireOrderPlanResult result)throws BusinessException {
		InquireOrderPlan item=new InquireOrderPlan();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			inquireOrderPlanDao.insert(item);
		}else {
			InquireOrderPlan tmp=inquireOrderPlanDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			inquireOrderPlanDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,InquireOrderPlanResult> getItemMap(Set<Integer> idSet){
		Map<Integer, InquireOrderPlanResult> map=new HashMap<Integer, InquireOrderPlanResult>();
		List<InquireOrderPlan> list=inquireOrderPlanDao.findByIds(StringUtil.setToList(idSet));
		List<InquireOrderPlanResult> resultList=DTOUtils.convertList(list, InquireOrderPlanResult.class);
		for(InquireOrderPlanResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,InquireOrderPlanResult> getItemMap(){
		Map<Integer, InquireOrderPlanResult> map=new HashMap<Integer, InquireOrderPlanResult>();
		InquireOrderPlanQuery param=new InquireOrderPlanQuery();
		List<InquireOrderPlan> list=inquireOrderPlanDao.page(param, 0, Integer.MAX_VALUE);
		List<InquireOrderPlanResult> resultList=DTOUtils.convertList(list, InquireOrderPlanResult.class);
		for(InquireOrderPlanResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
